home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / pool8.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  7.4 KB  |  308 lines

  1. #ifndef _global_h
  2. #    include "global.h"
  3. #endif
  4.  
  5. #ifndef _pool8_h
  6. #    include "pool8.h"
  7. #endif
  8. #ifndef _graph_h
  9. #    include "graph.h"
  10. #endif
  11. #ifndef _mover_h
  12. #    include "mover.h"
  13. #endif
  14. #ifndef _ball_h
  15. #    include "ball.h"
  16. #endif
  17.  
  18. Pool8::Pool8(double ft) :
  19.     Pool( ft*2.54*12, ft*2.54*6 )
  20. {
  21.     SelectTable(-1);
  22.  
  23.     black_col    = AddBallColor( "black" );
  24.     cueball=0;
  25.  
  26.     if (!deluxe) {
  27. // Pool-Kugeln: "Television"
  28.         full_col        = AddBallColor( "yellow" );
  29.         half_col        = AddBallColor( "red" );
  30.     }
  31.     else {
  32. // normale Pool-Kugeln
  33.         ball_col[0] = AddBallColor( "gold1" );            // yellow
  34.         ball_col[1] = AddBallColor( "blue" );            // blue
  35.         ball_col[2] = AddBallColor( "red" );            // red
  36.         ball_col[3] = AddBallColor( "DarkViolet" );    // dark violett
  37.         ball_col[4] = AddBallColor( "orange" );        // orange
  38.         ball_col[5] = AddBallColor( "MediumForestGreen" );    // dark green
  39.         ball_col[6] = AddBallColor( "brown" );            // brown
  40.     }
  41.  
  42.     mh = 0;
  43.     balls_in_pocket = 0;
  44. }
  45.  
  46. Pool8::~Pool8() {
  47.     if (cueball) {
  48.         delete cueball;
  49.         for (int i=0;i<15;i++)    delete ball[i];
  50.         cueball=0;
  51.     }
  52.     if (mh)        delete mh;
  53. }
  54.  
  55.  
  56. void Pool8::Triangle( double x, double y )
  57. {
  58. static t[15] = {        1,
  59.                         -1, -2,
  60.                       2,  0,  3,
  61.                   -3,  4, -4, -5,
  62.                  5, -6,  6,  7, -7 };
  63.  
  64. const int count = 5;
  65. int        c=0;
  66. double    cdist = sqrt( 3.*(GetNormalBallSize()+Offset)*(GetNormalBallSize()+Offset) );
  67. Ball    *r;
  68.  
  69. //    x-=(cdist*(count-1)/2);
  70.     for( int col=0; col<count; col++ ) {
  71.         for( int row=0; row<=col; row++ ) {
  72.             r=new Ball( x+col*cdist, y+(row-col/2.0)*2*(GetNormalBallSize()+Offset), 0, 0, GetNormalBallSize() );
  73.             if (t[c]>0) {
  74.                 if (!deluxe)
  75.                         r->state = new BallState( m, full_col, r->P() );
  76.                 else    r->state = new HalfBallState( m, ball_col[t[c]-1], r->P() );
  77.                 ball[t[c]-1] = r;
  78.             }
  79.             else if (t[c]<0) {
  80.                 if (!deluxe)
  81.                         r->state = new BallState( m, half_col, r->P() );
  82.                 else    r->state = new HalfBallState( mh, ball_col[-t[c]-1], r->P() );
  83.                 ball[8-t[c]-1] = r;
  84.             }
  85.             else {
  86.                 if (!deluxe)
  87.                         r->state = new BallState( m, black_col, r->P() );
  88.                 else    r->state = new HalfBallState( m, black_col, r->P() );
  89.                 ball[7] = r;
  90.             }
  91.             c++;
  92.         }
  93.     }
  94. }
  95.  
  96.  
  97. void Pool8::InitPlayground() {
  98.  
  99.     if (deluxe) {
  100.         mh = new HalfBallMover( GetNormalBallSize(), (deluxe==3)?2:4 );
  101.         mh->Init();
  102.         m  = new HalfBallMover( GetNormalBallSize(), (deluxe==2)?3:((deluxe==3)?1:0) );
  103.         m->Init();
  104.         BallRadius = m->GetActRadius()-Offset;
  105.     }
  106.     Pool::InitPlayground();
  107.  
  108.     tridef  = Vec2(AreaOffX()+AreaWidth()*0.75, AreaOffY()+AreaHeight()/2.);
  109.     Triangle(tridef);
  110.     cuedef  = Vec2(AreaOffX()+AreaWidth()*0.125,AreaOffY()+AreaHeight()*0.375);
  111.     cueball = new Ball( cuedef.X(), cuedef.Y(), RealZero, RealZero, BallRadius );
  112.     if (!deluxe)
  113.             cueball->state = new BallState( m, cue_col, cueball->P() );
  114.     else    cueball->state = new HalfBallState( m, cue_col, cueball->P() );
  115.  
  116. #ifdef DEBUG
  117.     if (deluxe) {
  118.         if (debug&ShowTurns)        mh->CreateTurnWindow();
  119.         if (debug&ShowRings)        mh->CreateRingWindow();
  120.     }
  121. #endif
  122. }
  123.  
  124.  
  125. void Pool8::DrawBackground() const {
  126.     Pool::DrawBackground();
  127.     SetBgColor(table_line_col);
  128.     DrawLine( AreaOffX()+AreaWidth()*0.25, AreaOffY(),
  129.                             AreaOffX()+AreaWidth()*0.25, AreaOffY()+AreaHeight() );
  130.     FillCircle( AreaOffX()+AreaWidth()*0.25, AreaOffY()+AreaHeight()*0.50, 1.0 );
  131.     FillCircle( AreaOffX()+AreaWidth()*0.75, AreaOffY()+AreaHeight()*0.50, 1.0 );
  132. }
  133.  
  134. ////////////////////////////////////////////////////////////////////////////
  135.  
  136. void Pool8::InPocket( Ball *b ) {
  137.     for (int i=0;i<15;i++) {
  138.         if (b==ball[i]) {
  139.             balls_in_pocket  |= (1<<i);
  140.             return;
  141.         }
  142.     }
  143.     Pool::InPocket(b);
  144. }
  145.  
  146. int Pool8::IsSelectable(Ball *b) {
  147.     for (int i=0;i<15;i++) {
  148.         if (b==ball[i]) {
  149.             if (balls_in_pocket & (1<<i)) {
  150.                 Vec2    newpos;
  151.                 b->FitsNextTo(tridef,Vec2(1.0,RealZero),&newpos);
  152.                 b->SetP(newpos);                    // closer to right wall
  153.  
  154.                 b->ChgV(Vec2Zero);
  155.                 balls_in_pocket &= ~(1<<i);    // it's back in the game
  156.  
  157.                 return 0;
  158.             }
  159.             else {
  160.                 return 1;
  161.             }
  162.         }
  163.     }
  164.  
  165.     return Pool::IsSelectable(b);
  166. }
  167.  
  168. // -------------------------------------------------------------------------
  169.  
  170. Pool8Demo::~Pool8Demo() {}
  171.  
  172. const Real &Pool8Demo::GetPresetA() const            { return PresetA; }
  173. const Real &Pool8Demo::GetSlowGranularity() const    { return SlowGranularity; }
  174.  
  175. void Pool8Demo::InitPlayground() {
  176.  
  177.     if (deluxe) {
  178.         mh = new HalfBallMover( GetNormalBallSize(), (deluxe==3)?2:4 );
  179.         mh->Init();
  180.         m  = new HalfBallMover( GetNormalBallSize(), (deluxe==2)?3:((deluxe==3)?1:0) );
  181.         m->Init();
  182.         BallRadius = m->GetActRadius()-Offset;
  183.     }
  184.     Billard::InitPlayground();
  185.     BallRadius = m->GetActRadius();
  186.     Billard::InitTable();
  187.  
  188.     Triangle(   AreaOffX()+AreaWidth()*0.75, AreaOffY()+AreaHeight()/2.);
  189.     cueball = new Ball( AreaOffX()+AreaWidth()*0.125, AreaOffY()+AreaHeight()*0.375,
  190.                     shot_speed, shot_speed, BallRadius );
  191.     if (!deluxe)
  192.             cueball->state = new BallState( m, cue_col, cueball->P() );
  193.     else    cueball->state = new HalfBallState( m, cue_col, cueball->P() );
  194. }
  195.  
  196. void Pool8Demo::DrawBackground() const {
  197.     Billard::DrawBackground();
  198.     SetBgColor(table_line_col);
  199.     DrawLine( AreaOffX()+AreaWidth()*0.25, AreaOffY(),
  200.                             AreaOffX()+AreaWidth()*0.25, AreaOffY()+AreaHeight() );
  201.     FillCircle( AreaOffX()+AreaWidth()*0.25, AreaOffY()+AreaHeight()*0.50, 1.0 );
  202.     FillCircle( AreaOffX()+AreaWidth()*0.75, AreaOffY()+AreaHeight()*0.50, 1.0 );
  203. }
  204.  
  205. // -------------------------------------------------------------------------
  206.  
  207. #ifdef DEBUG
  208.  
  209. #ifndef _wall_h
  210. #    include "wall.h"
  211. #endif
  212.  
  213.  
  214. class TestField {
  215.     public:
  216.         TestField( const Vec2 &p1, const Vec2 &p2, const Real &len,
  217.                             Pool8Test *d );
  218.         ~TestField();
  219.  
  220.         void Draw() const;
  221.         void Insert( TestField *n )    { n->next = next; next = n; }
  222.  
  223.     private:
  224.         Vec2        e[4];
  225.         Wall            *w[4];
  226.         int            nballs;
  227.         Ball            **b;
  228.         TestField    *next;
  229. };
  230.  
  231. TestField::TestField( const Vec2 &p1, const Vec2 &p2, const Real &len,
  232.         Pool8Test *d ) {
  233. Vec2    dist = (p2-p1);
  234. Vec2    dir_x = dist.TurnRight().Norm1();
  235. Vec2    dir_y = dist.Norm1();
  236.  
  237. const double SPEED = 20.0;
  238.  
  239.     next = 0l;
  240.     e[0] = p1;
  241.     e[1] = p1+dir_x*len;
  242.     e[2] = p2+dir_x*len;
  243.     e[3] = p2;
  244.     for (int i=0;i<4;i++)    w[i] = new Wall( e[i], e[(i+1)%4] );
  245.  
  246.     nballs = (int)(dist.Norm()/Pool8Test::BallRadius/3.);
  247.     b = new Ball*[nballs];
  248.     for (i=0;i<nballs;i++) {
  249.         Vec2 pos    = e[0]
  250.                         + 3.*Pool8Test::BallRadius*dir_x
  251.                         + Real((i+1)*3)*Pool8Test::BallRadius * dir_y;
  252.         b[i] = new Ball( pos.X()+((rand()%150)/100.0), pos.Y(),
  253.                         dir_x.X()*SPEED, dir_x.Y()*SPEED,
  254.                         Pool8Test::BallRadius );
  255.         b[i]->state = new HalfBallState( d->mh, d->ball_col[i%7], b[i]->P() );
  256.         ((HalfBallState*)b[i]->state)->st = d->mh->AngVec2St(0,i*d->mh->vecs_b/nballs);
  257.  
  258.     }
  259. }
  260.  
  261. TestField::~TestField() {
  262.     for (int i=0;i<nballs;i++)        delete b[i];
  263.     delete b;
  264.     for (i=0;i<4;i++)    delete w[i];
  265.     if (next)        delete next;
  266. }
  267.  
  268. void TestField::Draw() const {
  269.     for (int i=0;i<4;i++)    DrawLine( e[i], e[(i+1)%4] );
  270.     if (next)        next->Draw();
  271. }
  272.  
  273.  
  274. Pool8Test::~Pool8Test()
  275. {
  276.     delete field_queue;
  277. }
  278.  
  279. void Pool8Test::InitPlayground() {
  280.  
  281.     if (deluxe) {
  282.         mh = new HalfBallMover( GetNormalBallSize(), 2 );
  283.         mh->Init();
  284.         BallRadius = mh->GetActRadius()-Offset;
  285.     }
  286.     Billard::InitPlayground();
  287.     BallRadius = m->GetActRadius();
  288.     Billard::InitTable();
  289.  
  290.     Vec2    dir = (Mid(2)-Mid(0))*3.0/4.0;
  291.     if (shot_speed>=RealZero&&shot_speed<=90.)
  292.             dir=dir.TurnAngleDeg(-shot_speed);
  293.  
  294. #if (1)
  295.     field_queue = new TestField( Mid(0), Mid(0)+dir, AreaWidth()/4., this );
  296. #else
  297.     field_queue = new TestField( Edge(0), Edge(3), AreaWidth(), this );
  298.     field_queue->Insert( new TestField( Mid(2), Mid(1), AreaWidth()/5, this ) );
  299. #endif
  300. }
  301.  
  302. void Pool8Test::DrawBackground() const {
  303.     Pool8Demo::DrawBackground();
  304.     SetBgColor(marker_col);
  305.     field_queue->Draw();
  306. }
  307. #endif
  308.